package com.example.administrator.activity.parcitise.Storage.QQ;

import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.activity.R;

import java.util.HashMap;

public class SpQQActivity extends AppCompatActivity {
    EditText user;
    EditText pass;
    Button denglu;
    String filename="qq.txt";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inner_qq);
        init();
        String[] p={"android.permission.WRITE_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE"};
        ActivityCompat.requestPermissions(SpQQActivity.this,p,1);
        /**
         * 从sp/文件中获取账户密码并显示在输入框
         */
//        HashMap<String,String> hashMap=SPSaveqq.get(SpQQActivity.this);
        HashMap<String,String> hashMap=OutQQActivity.get(SpQQActivity.this);
        user.setText(hashMap.get("name"));
        pass.setText(hashMap.get("pwd"));
        View.OnClickListener clickListener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * 1.若输入框为空，则Toast提示不能空
                 * 2.若不为空，将账号密码保存，sp file
                 */
                String name=user.getText().toString();
                String pwd=pass.getText().toString();

                if(TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
                    Toast.makeText(SpQQActivity.this, "账户和密码不能空", Toast.LENGTH_SHORT).show();
                }else {
                    /**
                     * 调用SpSaveQQ中的方法完成，将账户密码保存sp
                     */
//                    boolean flag=SPSaveqq.save(SpQQActivity.this,name,pwd);
                    boolean flag=OutQQActivity.save(SpQQActivity.this,name,pwd);
                    if (flag){

                        Toast.makeText(SpQQActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(SpQQActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
                    }

                }
            }
        };
        user.setOnClickListener(clickListener);
        pass.setOnClickListener(clickListener);
        denglu.setOnClickListener(clickListener);
    }


    private void init() {
        user=findViewById(R.id.edituser);
        pass=findViewById(R.id.editpass);
        denglu=findViewById(R.id.denglu);
    }
}
